import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:form_builder_validators/form_builder_validators.dart';

import '../theme/custom_theme.dart';

class VTextField extends StatelessWidget {
  final String keyName;
  final String? labelText;
  final String? placeholder;
  final FormFieldValidator? validators;
  final TextInputType textInputType;
  final bool isPwd;
  final bool hasPaddingBottom;
  final String? initialValue;
  final bool? isRequired;

  const VTextField({
    Key? key,
    required this.keyName,
    this.placeholder,
    this.validators,
    this.textInputType = TextInputType.text,
    this.isPwd = false,
    this.labelText,
    this.hasPaddingBottom = true,
    this.initialValue,
    this.isRequired,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        if (labelText != null)
          RichText(
            textHeightBehavior: const TextHeightBehavior(
              leadingDistribution: TextLeadingDistribution.even,
            ),
            text: TextSpan(
              children: [
                if (isRequired == true) TextSpan(
                  text: '* ',
                  style: TextStyle(
                    color: Colors.red,
                    fontSize: 15.sp,
                  )
                ),
                TextSpan(
                  text: labelText!,
                  style: TextStyle(
                    fontSize: 15.sp,
                    height: 2.0,
                  ),
                ),
              ],
            ),
          ),
        FormBuilderTextField(
          initialValue: initialValue,
          obscureText: isPwd,
          name: keyName,
          keyboardType: textInputType,
          decoration: InputDecoration(
            hintText: placeholder,
            hintStyle: TextStyle(
                fontSize: 12.sp, color: CustomTheme.colorSchema.neutral),
            fillColor: CustomTheme.colorSchema.tertiary,
            filled: true,
            border: InputBorder.none,
          ),
          validator: validators,
        ),
        if (hasPaddingBottom) SizedBox(height: 10.h)
      ],
    );
  }
}
